package com.zygh.lz.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zygh.lz.entity.InformationUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface InformationUserMapper extends BaseMapper<InformationUser> {

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    List<InformationUser> listAll();


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    InformationUser getById(Integer id);

    /**
     * 新增，插入所有字段
     *
     * @param informationUser 新增的记录
     * @return 返回影响行数
     */
    int insert(InformationUser informationUser);

    /**
     * 新增，忽略null字段
     *
     * @param informationUser 新增的记录
     * @return 返回影响行数
     */
    int insertIgnoreNull(InformationUser informationUser);

    /**
     * 修改，修改所有字段
     *
     * @param informationUser 修改的记录
     * @return 返回影响行数
     */
    int update(InformationUser informationUser);

    /**
     * 修改，忽略null字段
     *
     * @param informationUser 修改的记录
     * @return 返回影响行数
     */
    int updateIgnoreNull(InformationUser informationUser);

    /**
     * 删除记录
     *
     * @param informationUser 待删除的记录
     * @return 返回影响行数
     */
    int delete(InformationUser informationUser);


    int finalReading(String acceptWarningSignal);

    List<InformationUser> findInforUserById(Integer id);

    List<InformationUser> findInforUserReadById(@Param("id") Integer id,
                                                @Param("isRead") Integer isRead);

    List<Map> findInforUserByWarningSignal(@Param("warningSignal") String warningSignal,
                                           @Param("startTime") String startTime,
                                           @Param("endTime") String endTime);


    @Select("<script> " +
            "SELECT iu.*,i.msgLevel FROM information_user iu LEFT JOIN information i ON iu.information_id=i.id \n" +
            "<where> " +
            "<if test='code != null'>" +
            "iu.accept_warning_signal = #{code} " +
            "</if>" +
            "<if test='msgLevel != null'>" +
            "AND i.msgLevel = #{msgLevel}" +
            "</if>" +
            "<if test='endTime != null and startTime !=null'> " +
            "and iu.update_time BETWEEN #{startTime} and #{endTime}" +
            "</if>" +
            "<if test='isRead != null'>" +
            " AND iu.is_read = #{isRead}" +
            "</if>" +
            "</where>" +
            "</script>")
    List<Map> findInforByCode(@Param("code") String code,
                              @Param("msgLevel") String msgLevel,
                              @Param("startTime") String startTime,
                              @Param("endTime") String endTime,
                              @Param("isRead") Integer isRead);

    @Select("<script>" +
            "SELECT count(*) FROM information_user i LEFT JOIN staff s ON i.accept_warning_signal = s.staff_num LEFT JOIN " +
            "section sc ON s.sys_section_id = sc.sys_section_id " +
            "<where>" +
            "<if test='id != null'>" +
            " sc.section_pid = #{id}" +
            "</if>" +
            "  AND update_time BETWEEN #{beginDay} and #{endDay}" +
            "</where>" +
            "</script>")
    Integer dispatch(@Param("id") Integer id,
                     @Param("beginDay") String beginDay,
                     @Param("endDay") String endDay);

}